package cn.elricyu.tt.config;

import java.util.Collections;
import java.util.List;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.elricyu.tt.util.SystemUtil;

/**
 * @author elricyu
 * 
 */
public class DefaultConfig {

	private static final Log logger = LogFactory.getLog(DefaultConfig.class);

	private static final Configuration config = getConfiguration();

	protected static Configuration getConfiguration() {
		ConfigurationFactory factory = new ConfigurationFactory();
		String fileName = SystemUtil.getUserConfigDir() + "gougou.xml";
		try {
			factory.setConfigurationFileName(fileName);
			return factory.getConfiguration();
		} catch (Exception e) {
			logger.fatal(e.getMessage() + fileName, e);
			throw new RuntimeException(e.getMessage() + fileName, e);
		}
	}

	public Object getProperty(String key) {
		return config.getProperty(key);
	}

	public String getString(String key) {
		return config.getString(key);
	}

	public String getString(String key, String defaultValue) {
		return config.getString(key, defaultValue);
	}

	public int getInt(String key) {
		return config.getInt(key);
	}

	public long getLong(String key) {
		return config.getLong(key);
	}

	public int getInt(String key, int defaultValue) {
		return config.getInt(key, defaultValue);
	}

	@SuppressWarnings("unchecked")
	public List<String> getList(String key) {
		return config.getList(key, Collections.emptyList());
	}

	public double getDouble(String key, double defaultValue) {
		return config.getDouble(key, defaultValue);
	}

	public boolean getBoolean(String key, boolean defaultValue) {
		String v = config.getString(key);
		if (StringUtils.isBlank(v)) {
			return defaultValue;
		}
		return ("true".equalsIgnoreCase(v) || "1".equals(v));
	}
}
